package ChatDemo.server.Handler;

import ChatDemo.message.GroupQuitRequestMessage;
import ChatDemo.message.GroupQuitResponseMessage;
import ChatDemo.server.session.Group;
import ChatDemo.server.session.GroupSessionFactory;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

@ChannelHandler.Sharable
public class GroupQuitRequestMessageHandler extends SimpleChannelInboundHandler<GroupQuitRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupQuitRequestMessage msg) throws Exception {
        Group group = GroupSessionFactory.getGroupSession().removeMember(msg.getGroupName(), msg.getUsername());
        if (group==null){
                ctx.writeAndFlush(new GroupQuitResponseMessage(false,"不存在该群！"));
        }else {
                ctx.writeAndFlush(new GroupQuitResponseMessage(true,"success!"));
        }
    }
}
